package com.gitee.linmt.config;

import com.gitee.linmt.advice.ResultAdvice;
import com.gitee.linmt.advice.ServiceExceptionAdvice;
import com.gitee.linmt.cache.CacheResultMessage;
import com.gitee.linmt.init.InitMessage;
import com.gitee.linmt.properties.ResultProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

/**
 * Result 配置.
 *
 * @author <a href="mailto:lin-mt@outlook.com">lin-mt</a>
 */
@Configuration
@EnableConfigurationProperties(ResultProperties.class)
public class ResultConfig<T> {
    
    private final ResultProperties resultProperties;
    
    public ResultConfig(final ResultProperties resultProperties) {
        this.resultProperties = resultProperties;
    }
    
    @Bean
    @ConditionalOnMissingBean(InitMessage.class)
    public InitMessage<T> initResultMessage() {
        return new InitMessage<>();
    }
    
    @Bean
    @ConditionalOnClass(InitMessage.class)
    public CacheResultMessage<T> cacheResultMessage() {
        return new CacheResultMessage<>(resultProperties);
    }
    
    @Bean
    @Order(-2)
    @ConditionalOnMissingBean(ResultAdvice.class)
    public ResultAdvice<T> resultAdvice() {
        return new ResultAdvice<>();
    }
    
    @Bean
    @Order(-1)
    @ConditionalOnMissingBean(ServiceExceptionAdvice.class)
    public ServiceExceptionAdvice exceptionAdvice() {
        return new ServiceExceptionAdvice();
    }
}
